var lookid;
layui.use(['form','layer','laydate','table','rate','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;
        rate = layui.rate;
        
    //场景列表
    var tableIns = table.render({
        elem: '#callRatioList',
        method: 'post',
        url : '../callRatio/dataGrid',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        limits : [10,15,20,25],
        id : "callRatioListTable",
        cols : [[
            {field: 'id', title: '序号', width:60, align:"center"},
            {field: 'name', title: '场景名称', width:320},  
            {field: 'createTime', title: '发布时间', minWidth:150, align:"center"},
            {title: '评分', width:170, field:'score',fixed:"right",align:"center", templet:function(d){
            	var txt = '未评分';
            	if(d.score=='1'){
            		txt='极差';
            	}else if(d.score==2){
            		txt='差';
            	}else if(d.score==3){
            		txt='一般';
            	}else if(d.score==4){
            		txt='好';
            	}else if(d.score==5){
            		txt='极好';
            	}
                return txt;
            }},
            {title: '操作', width:220, templet:'#callRatioListBar',fixed:"right",align:"center"}
        ]]
    });
    var grateId;
    var score;

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("callRatioListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    name: $(".searchVal").val().trim()  //搜索的关键字
                }
            })
        }else{
            //layer.msg("请输入搜索的内容");
            tableIns.reload();
        }
    });

    //列表操作
    table.on('tool(callRatioList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            editScenes(data);
        } else if(layEvent === 'grate'){
        	grateScenes(data);
        } else if(layEvent === 'del'){ //删除
            var id = data.id;
            layer.confirm('确定删除此场景？',{icon:3, title:'提示信息'},function(index){
    			$.ajax({
      			type : "POST",
      			url : "../callRatio/delete",
      			data : {
      				"id" : id
      			},
      			success : function() {
      				top.layer.msg("场景信息删除成功！");
      	 			tableIns.reload();
                    layer.close(index);
      			}
    			});
            });
         } else if(layEvent === 'look'){ //查看
        	lookid = data.id;
            var index = layui.layer.open({
            title : "数据展示",
            type : 2,
            content : "../callRatio/showResult",
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回场景列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
        }
    });

    //添加场景
    function addCallRatioScenes(){
        var index = layui.layer.open({
            title : "添加场景",
            type : 2,
            content : "../callRatio/addCallRatio",
            success : function(layero, index){
                setTimeout(function(){
                    layui.layer.tips('点击此处返回场景列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addCallRatio_btn").click(function(){
        addCallRatioScenes();
    })
    
    //编辑场景
    function editScenes(edit){
        var index = layui.layer.open({
            title : "编辑渠道",
            type : 2,
            content : "../callRatio/editPage?id=" + edit.id,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                form.render();
                setTimeout(function(){
                    layui.layer.tips('点击此处返回渠道列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    
    //场景评分
    function grateScenes(grate){
    	$("#grateId").val(grate.id);
    	$("#score").val(grate.score);
        var index = layui.layer.open({
            title : "场景评分",
            type : 2,
            area: ['480px', '200px'],
            content : "../callRatio/gratePage",
            end: function () {
                location.reload();
            },
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                form.render();
                setTimeout(function(){
                },500)
            }
        })    
    }

})